import edu.princeton.cs.introcs.In;
import edu.princeton.cs.introcs.StdIn;
import edu.princeton.cs.introcs.StdOut;
import stat.*;
import tsd.TSD;
import tsd.TSDFile;

import java.util.ArrayList;
import java.util.List;

/*
    Code-style: http://www.oracle.com/technetwork/java/codeconv-138413.html
 */

public class Main {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        ParamParser params = new ParamParser(args);
        if (!params.state) {
            return;
        }

        // read and parse input file
        In in = new In(params.inputFile);
        StdOut.println("Parsing input file...");
        ArrayList<TSD> tsdList = TSDFile.readCSV(in);
        in.close();
        StdOut.println(tsdList.size() + " records parsed");

        // print quick help
        StdOut.println("\nQuick help");
        StdOut.println("You may specify filter as regular expression.");
        StdOut.println("Examples:");
        StdOut.printf("%-15s%s\n", ".*", "- all entries (filter off)");
        StdOut.printf("%-15s%s\n", "l1hs", "- only L1HS repeats");
        StdOut.printf("%-15s%s\n", "l1hs|l1m1", "- L1HS or L1M1 repeats");
        StdOut.println("Type \":quit\" for exit\n");

        // create charts
        TSDStat stat = new TSDStat(tsdList);
        List<TSDStatChart> chartList = new ArrayList<TSDStatChart>();
        chartList.add(new TSDLengthChart(100, 100));
        chartList.add(new TSDDistChart(700, 100));
        chartList.add(new TSDConservChart(1300, 100));
        for (TSDStatChart chart : chartList) {
            stat.addObserver(chart);
        }
        stat.notifyObservers();

        // ask filter until exit
        String req;
        do {
            StdOut.print("Type filter or \":quit\" for exit: ");
            req = StdIn.readLine();
            stat.filter(req);
            stat.notifyObservers();

        } while (!req.equals(":quit"));

        for (TSDStatChart chart : chartList) {
            chart.close();
        }
    }
}
